package ecs.core;

import ecs.net.EventType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

public class EventCenter<T> {

    private final HashMap<Integer, List<Consumer<T>>> _handlers = new HashMap<>();

    public void register(int type, Consumer<T> handler)
    {
        if(!_handlers.containsKey(type))
            _handlers.put(type, new ArrayList<>());
        _handlers.get(type).add(handler);
    }

    public void unregister(int type, Consumer<T> handler)
    {
        if(_handlers.containsKey(type))
        {
            _handlers.get(type).remove(handler);
        }
    }

    public void send(int event, T param)
    {
        List<Consumer<T>> handlers = _handlers.get(event);
        if(handlers!=null)
            handlers.forEach(handler->{
                handler.accept(param);
            });
    }
}
